package cn.bearspark.api.controller.main;

import cn.bearspark.api.service.main.CategoryService;
import cn.bearspark.api.vo.main.category.AddCategoryVO;
import cn.bearspark.api.vo.main.category.CategorySimpleVO;
import cn.bearspark.api.vo.main.category.CategoryVO;
import cn.bearspark.api.vo.main.category.ModifyCategoryVO;
import cn.bearspark.framework.web.starter.vo.ResponseVO;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 专栏的分类模块
 *
 * @author f
 */
@Validated
@RestController
@RequestMapping("/category")
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 添加专栏分类
     * <p>
     * 个人专栏分类最多 100 个，如果超过 100 个，则不会添加
     *
     * @param addCategoryVO 添加专栏分类的请求对象
     * @return 响应对象
     */
    @PostMapping
    public ResponseVO<Object> add(@Valid @RequestBody AddCategoryVO addCategoryVO) {
        categoryService.add(addCategoryVO);
        return ResponseVO.success();
    }

    /**
     * 获取专栏分类信息
     *
     * @param categoryId 待获取信息的专栏分类 id
     * @return 专栏分类信息
     */
    @GetMapping("/{categoryId}")
    public ResponseVO<CategoryVO> get(@PathVariable Long categoryId) {
        return ResponseVO.success(categoryService.get(categoryId));
    }

    /**
     * 修改专栏分类
     *
     * @param modifyCategoryVO 修改专栏分类的请求对象
     * @return 响应对象
     */
    @PutMapping
    public ResponseVO<Object> modify(@Valid @RequestBody ModifyCategoryVO modifyCategoryVO) {
        categoryService.modify(modifyCategoryVO);
        return ResponseVO.success();
    }

    /**
     * 删除专栏分类
     *
     * @param categoryId 待删除的专栏分类 id
     * @return 响应对象
     */
    @DeleteMapping("/{categoryId}")
    public ResponseVO<Object> delete(@PathVariable Long categoryId) {
        categoryService.delete(categoryId);
        return ResponseVO.success();
    }

    /**
     * 列举专栏分类简要信息
     * <p>
     * 按照分类专栏数量进行排序
     *
     * @param listedUserId 待列举专栏分类的用户 id
     * @return 专栏分类简要信息的集合
     */
    @GetMapping("/list")
    public ResponseVO<List<CategorySimpleVO>> list(@NotNull Long listedUserId) {
        return ResponseVO.success(categoryService.list(listedUserId));
    }

}
